package Server;

import Server.Domain.Message;
import Utility.Scan;

import java.io.IOException;

import static Server.ServerView.serverSocket;
import static Server.ServerView.serverTaskData;

public class ServerManager implements Runnable {


    @Override
    public void run() {
        while (true) {
            System.out.println("请输入命令:1 推送消息; 2 关闭服务器");
            String str = Scan.read();
            if (str.equals("1")) {
                System.out.println("请输入推送的消息");
                Message message = new Message();
                message.setContent(Scan.read());
                for (Task task : serverTaskData.getAllTasks()) {
                    System.out.println("发给" + task.user.getName() + "的消息传达中..");
                    task.send(message);
                }
            }
            else if (str.equals("2")) {
                System.out.println("确定关闭?(y/n)");
                if (Scan.read().equals("y")) {
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    System.exit(0);
                    break;
                }
            }
        }
    }
}
